package github.ziwukun.jsonql.orm;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonAlias;

import lombok.Data;

/**容器属性
 * @author Hugo.Yu
 */
@Data
class CtnPropBuilder {
	// 是否是id属性，有且仅有一个
	@JsonAlias({"id"})
	boolean id = false;
	// 模型属性名称
	@JsonAlias({"prop","propName"})
	String propName;
	// 容器的字段名
	@JsonAlias({"field","fieldName"})
	String fieldName;
	// 是否是单一属性 单一属性只有一个值
	@JsonAlias({"one","single"})
	boolean single = true;
	// 支持的查询方式
	// TODO 不支持设置查询条件如何处理？？？？
	@JsonAlias({"support","supportConditions"})
	Set<SupportCondition> supportConditions = new HashSet<>();
	// 是否支持排序
	@JsonAlias({"sort","sortable"})
	boolean sortable = true;
	// 类型转换器
	@JsonAlias({"converter"})
	Class<? extends PropConverter<?>> converter = DefaultPropConverter.class;
	// 子属性设置
	@JsonAlias({"childs","childProps"})
	List<CtnPropBuilder> childProps = new LinkedList<>();
	
	public CtnPropImpl toCtnProp(ModelImpl ofModel) {
		return new CtnPropImpl(this,ofModel);
	}
	
	public JoinPropImpl toJoinProp(ModelImpl ofModel,JoinPropCtnImpl ofCtn,
		CtnField ctnField) {
		return new JoinPropImpl(this,ofModel,ofCtn,ctnField);
	}
	
	public CtnFieldImpl toCtnField() {
		return new CtnFieldImpl(this);
	}
	
	
	public static void instanceCtnProp(CtnPropBuilder builder,CtnPropImpl ctnProp,ModelImpl ofModel) {
		OrmAssert.notBlank(builder.propName, "属性名称不能为空");
		OrmAssert.notBlank(builder.fieldName, "字段名称不能为空");
		OrmAssert.notBlank(builder.converter, "类型转换方式不能为空");
		
		ctnProp.propName = builder.propName;
		ctnProp.single = builder.single;
		ctnProp.supportConditions.addAll(builder.supportConditions);
		// 有子字段定义或者有多个字段值 则不能参与排序
		ctnProp.sortable = !builder.childProps.isEmpty() || !ctnProp.single?
			false:builder.sortable;
		ctnProp.converter = builder.converter;
		if(builder.supportConditions.isEmpty()) {
			ctnProp.supportConditions.add(SupportCondition.ALL);
		}
		else {
			ctnProp.supportConditions.addAll(builder.supportConditions);
		}
		
		ctnProp.ofModel = ofModel;
		ctnProp.ctnField = builder.toCtnField();
		for (CtnPropBuilder childProp : builder.childProps) {
			ctnProp.childProps.add(childProp.toCtnProp(ofModel));
		}
	}
	
	public static void instanceCtnField(CtnPropBuilder builder,CtnFieldImpl ctnField) {
		OrmAssert.notBlank(builder.fieldName, "字段名称不能为空");
		
		ctnField.fieldName = builder.fieldName;
		for (CtnPropBuilder childProp : builder.childProps) {
			ctnField.childFields.add(childProp.toCtnField());
		}
	}
	
	public static void instanceJoinProp(
		CtnPropBuilder ctnPropBuilder,JoinPropImpl joinProp,
		ModelImpl ofModel,JoinPropCtnImpl ofCtn,CtnField ctnField) {
		
		OrmAssert.notBlank(ctnPropBuilder.propName, "属性名称不能为空");
		OrmAssert.notBlank(ctnPropBuilder.fieldName, "字段名称不能为空");
		OrmAssert.notBlank(ctnPropBuilder.converter, "类型转换方式不能为空");
		
		joinProp.propName = ctnPropBuilder.propName;
		joinProp.single = ctnPropBuilder.single;
		joinProp.supportConditions.addAll(ctnPropBuilder.supportConditions);
		joinProp.sortable = ctnPropBuilder.sortable;
		joinProp.converter = ctnPropBuilder.converter;
		joinProp.ofModel = ofModel;
		for (CtnPropBuilder childProp : ctnPropBuilder.childProps) {
			joinProp.childProps.add(childProp.toCtnProp(ofModel));
		}
		
		joinProp.ofCtn = ofCtn;
		joinProp.ctnField = ctnField;
	}
}
